package com.woniu.cq.medical.service.impl;

import com.woniu.cq.medical.common.DateUtil;
import com.woniu.cq.medical.common.PageResult;
import com.woniu.cq.medical.common.Result;
import com.woniu.cq.medical.controller.form.AttendanceRecordForm;
import com.woniu.cq.medical.controller.form.DutyQueryForm;
import com.woniu.cq.medical.entity.Duty;
import com.woniu.cq.medical.dao.DutyMapper;
import com.woniu.cq.medical.service.DutyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-09-05
 */
@Service
public class DutyServiceImpl extends ServiceImpl<DutyMapper, Duty> implements DutyService {

    @Autowired
    private DutyMapper dutyMapper;

    /**
     * 取得当前往后7天的的排班时间
     * @param doctorId
     * @return
     */
    @Override
    public List<Date> queryDutyDateList(Integer doctorId) throws ParseException {
        Date startTime = DateUtil.getCurrentTime(new Date());
        Date endTime = DateUtil.getWeekSunday();
        List<Date> dates = dutyMapper.queryDutyDateList(doctorId,startTime,endTime);
        return dates;
    }

    /**
     * 查询该医生所有的排班表
     * @param doctorId
     * @return
     */
    @Override
    public List<Duty> queryDutyList(Integer doctorId) {
        List<Duty> duties = dutyMapper.queryDutyList(doctorId);
        return duties;
    }

    /**
     * 根据医生ID 和 挂号时间 查出医生当天排班表记录
     * @param doctorId
     * @param dutyDate
     * @return
     */
    @Override
    public Duty queryDutyFill(Integer doctorId, LocalDate dutyDate) {
        Duty duty = dutyMapper.queryDutyOne(doctorId, dutyDate);
        return duty;
    }

    /**
     *  根据排班表ID修改预约时间
     * @param
     * @return
     */
    @Override
    public int updateNowBookPeople(Integer nowBookPeople,Integer dutyId) {
        int i = dutyMapper.updateNowBookPeople(nowBookPeople, dutyId);
        return i;
    }

    @Override
    public PageResult pageQuery(int pageIndex, int pageSize, String administrativeName, String doctorName, Date dutyDateBefore, Date dutyDateAfter) {
        long totalCount=dutyMapper.totalCount(administrativeName,doctorName,dutyDateBefore, dutyDateAfter);
        int pageStart=(pageIndex-1)*pageSize;
        List<DutyQueryForm> list=dutyMapper.pageQuery(pageStart,pageSize,administrativeName,doctorName,dutyDateBefore, dutyDateAfter);
        return PageResult.success(totalCount,pageIndex,pageSize,list);
    }

    @Override
    @Transactional
    public Result modifyDoctorDuty(DutyQueryForm dutyQueryForm) {
        int dutyId = dutyQueryForm.getDutyId();
        int nowBookPeople = dutyQueryForm.getNowBookPeople();
        LocalDate date = dutyQueryForm.getDutyDate();
        LocalDate now = LocalDate.now();
        Result result=null;
        Duty duty = dutyMapper.selectById(dutyId);
        if(date.isAfter(now)){
            if(duty.getDutyDate().isAfter(now)){
                if(!date.isEqual(duty.getDutyDate())){
                    if(nowBookPeople>0){
                        result=Result.fail("4001","当前已有人预约，不能更改"+dutyQueryForm.getDoctorName()+"医生的值班日期");
                    }else{
                        duty.setBookPeopleMax(dutyQueryForm.getBookPeopleMax());
                        duty.setDutyDate(dutyQueryForm.getDutyDate());
                        dutyMapper.updateById(duty);
                        result=Result.success();
                    }
                }else {
                    duty.setBookPeopleMax(dutyQueryForm.getBookPeopleMax());
                    dutyMapper.updateById(duty);
                    result=Result.success();
                }
            }else {
                result=Result.fail("5001","不能修改为过去的日期!!!");
            }
        }else {
            result=Result.fail("5001","不能修改过往的排班信息!!!");
        }
        return result;
    }
}
